package cn.demoncat.util.dingtalk.config;

import cn.demoncat.util.dingtalk.codec.DingTalkCallbackCrypto;
import com.aliyun.teaopenapi.models.Config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;

/**
 * 钉钉配置
 *
 * @author 延晓磊
 * @since 2023年03月23日
 */
@Configuration
public class DingTalkConfig {

    @Resource
    private DingTalkProperties dingTalkProperties;

    /**
     * 授权客户端
     *
     * @return
     *
     * @author 延晓磊
     * @since 2023年03月23日
     */
    @Bean
    public com.aliyun.dingtalkoauth2_1_0.Client dingTalkAuthClient(){
        Config config = new Config();
        config.protocol = "https";
        config.regionId = "central";
        try {
            return new com.aliyun.dingtalkoauth2_1_0.Client(config);
        } catch (Exception e) {
            throw new RuntimeException("构建钉钉授权客户端失败", e);
        }
    }

    /**
     * 工作流客户端
     *
     * @return
     *
     * @author 延晓磊
     * @since 2023年03月23日
     */
    @Bean
    public com.aliyun.dingtalkworkflow_1_0.Client dingTalkWorkflowClient() {
        Config config = new Config();
        config.protocol = "https";
        config.regionId = "central";
        try {
            return new com.aliyun.dingtalkworkflow_1_0.Client(config);
        } catch (Exception e) {
            throw new RuntimeException("构建钉钉工作流客户端失败", e);
        }
    }

    /**
     * 加密工具
     *
     * @return
     *
     * @author 延晓磊
     * @since 2023年03月23日
     */
    @Bean
    public DingTalkCallbackCrypto dingTalkCallbackCrypto(){
        return new DingTalkCallbackCrypto(dingTalkProperties.getEventToken(), dingTalkProperties.getEventKey(), dingTalkProperties.getAppKey());
    }
}
